package com.itheima.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @description <p>频道信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.admin.service.impl
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    /**
     * 频道分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResultVo findPage(ChannelPageRequestDto dto) {
          //1.设置分页参数
        Page<AdChannel> pageInfo = new Page<>(dto.getPage(), dto.getSize());
        //2.设置查询条件
        LambdaQueryWrapper<AdChannel> lqw = new LambdaQueryWrapper<>();
        lqw.like(dto.getName()!=null,AdChannel::getName,dto.getName());
        lqw.eq(dto.getStatus()!=null,AdChannel::getStatus,dto.getStatus());
        //3.执行查询
        IPage<AdChannel> pageResult = page(pageInfo, lqw);
        //4.封装结果返回
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     *新增频道
     * @param adChannel
     */
    @Override
    public void add(AdChannel adChannel) {
        adChannel.setIsDefault(0);
        adChannel.setCreatedTime(LocalDateTime.now());
        save(adChannel);
    }
}